package com.hnkywl.sts.service.sys;

import com.google.gson.Gson;
import com.hnkywl.sts.dao.htgl.CghtDao;
import com.hnkywl.sts.dao.htgl.HtDao;
import com.hnkywl.sts.dao.organ.BmfwDao;
import com.hnkywl.sts.dao.sys.ZdjDao;
import com.hnkywl.sts.dao.xsgl.KhDao;
import com.hnkywl.sts.entity.htgl.Cght;
import com.hnkywl.sts.entity.htgl.Htmx;
import com.hnkywl.sts.entity.htgl.Xsht;
import com.hnkywl.sts.entity.organ.Bmfw;
import com.hnkywl.sts.entity.sys.Yg;
import com.hnkywl.sts.entity.sys.Zdj;
import com.hnkywl.sts.entity.xsgl.Kh;
import com.hnkywl.sts.util.BaseUtils;
import net.ruixin.orm.hibernate.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * Created with IntelliJ IDEA.
 * User: ccr
 * Date: 13-5-20
 * Time: 下午7:40
 * To change this template use File | Settings | File Templates.
 */
@Service
public class ZdjService {
    private ZdjDao zdjDao;
    private KhDao khDao;
    private HtDao htDao;

    @Transactional
    public String findAllZdjList( String pmName,Date start,Date endDate,int pageNo, int pageSize) {
        Page<Zdj> zdjPage = this.zdjDao.findAllZdjList(pmName,start,endDate,pageNo, pageSize);
        List<Zdj> zdjList = zdjPage.getResult() == null ? new ArrayList<Zdj>() : zdjPage.getResult();
        String zdjJson = BaseUtils.fetchCollectionToJson(zdjList, "id", "pm.name","zdj","drrq","dateCreated","lastUpdated","mj.qc");
        return "{\"total\":" + zdjPage.getTotalCount() + ",\"rows\":[" + zdjJson + "]}";
    }

    public Zdj findZdjDetial(Long zdjId){
        return this.zdjDao.get(zdjId);
    }
    @Transactional
    public void saveZdj(Zdj zdj){
        this.zdjDao.save(zdj);
    }
    @Transactional
    public void saveZdj(Long zdjId,Double zdj){
        Zdj z = zdjDao.get(zdjId);
        z.setZdj(zdj);
        z.setLastUpdated(new Date());
        this.zdjDao.save(z);
    }
    @Transactional
    public void deleteZdj(Zdj zdj){
        this.zdjDao.delete(zdj);
    }
    @Transactional
    /**
     * * 1.每日斯迪尔现货指导价 数据库取 如果没有就不算了  提示哪天没有控货值 X
     * 2 合同订立当日斯迪尔现货指导价 没有就不算了 提示哪天没有控货值 Y
     * 3.合同签订的价格 =合同的含税价 A
     * 4.控货单价= X>= Y? A : A*X/Y
     */
    public void jsWrkKhKhdj(){
         List<Kh> khList = khDao.findWckKh();
        for(Kh kh :khList){
            Xsht xsht  = new Xsht() ;
            List<Xsht> xshtList=kh.getCgd().getCght().getZddhxshtList();
            for(Xsht xsht1 :xshtList){
                if(xsht1.getHzf().getId()==kh.getCggs().getId()){
                    xsht = (Xsht)htDao.get(xsht1.getId()) ;
                }
            }

            Double htHsj;
            //当日指导价
            Zdj drZdj = zdjDao.findZdjByPmAndDrrq( kh.getJtwz().getPm().getId(),new Date());
            //合同签订当日指导价
            Zdj htZdj = zdjDao.findZdjByPmAndDrrq(kh.getJtwz().getPm().getId(), xsht.getQdrq());
            //通过卡号寻找合同明细合同的含税价
            List<Htmx> htmxList =  kh.getXsmxList().get(0).getXsd().getXsht().getHtmxList();
            for(Htmx htmx :htmxList){
                if(htmx.getWzgg() == kh.getJtwz()){
                    //求得合同的含税价
                     htHsj = htmx.getHsj();
                    if(drZdj!=null && htZdj !=null && htHsj != 0 ){
                        Double khdj;
                        if(drZdj.getZdj() > htZdj.getZdj()){
                            khdj = htHsj;
                        }else{
                            khdj = htHsj*(drZdj.getZdj())/(htZdj.getZdj());
                        }
                        kh.setKhdj(khdj);
                        khDao.save(kh);
                    }
                }
            }
        }
    }

    @Autowired
    public void setZdjDao(ZdjDao zdjDao) {
        this.zdjDao = zdjDao;
    }
    @Autowired
    public void setKhDao(KhDao khDao) {
        this.khDao = khDao;
    }
    @Autowired
    public void setHtDao(HtDao htDao) {
        this.htDao = htDao;
    }
}
